﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DrugsShopManagement.DBCLASS;

namespace DrugsShopManagement
{
    public partial class FrmDrugsProvider : DevExpress.XtraEditors.XtraForm
    {
        public FrmDrugsProvider()
        {
            InitializeComponent();
        }

        Operator op = new Operator();

        private void FrmDrugsProvider_Load(object sender, EventArgs e)
        {

            this.WindowState = FormWindowState.Maximized;
            try
            {
                DataTable dt = new DataTable();
                string sql = "select providerId 供应商ID,providerCompany 供应商名称,providerPY 拼音码,providerAddress 供应商地址,providerTel 电话,providerName 联系人,providerWebsite 供应商网址,providerMailBox 供应商邮箱 from drugsProvider order by providerId";
                dt = op.getDataTable(sql, "drugsProvider");
                dataGridView1.DataSource = dt;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
            try
            {
                DataTable dt_1 = new DataTable();
                dt_1 = op.getDataTable("select * from drugsProvider where providerId='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'", "users");
                this.txtBoxProviderID.Text = dt_1.Rows[0]["providerId"].ToString();
                this.txtBoxProviderCompany.Text = dt_1.Rows[0]["providerCompany"].ToString();
                this.txtBoxProviderPY.Text = dt_1.Rows[0]["providerPY"].ToString();
                this.txtBoxProviderAdd.Text = dt_1.Rows[0]["providerAddress"].ToString();
                this.tetBoxProviderTel.Text = dt_1.Rows[0]["providerTel"].ToString();
                this.txtBoxProviderName.Text = dt_1.Rows[0]["providerName"].ToString();
                this.txtBoxProviderWeb.Text = dt_1.Rows[0]["providerWebsite"].ToString();
                this.txtBoxProviderMail.Text = dt_1.Rows[0]["providerMailBox"].ToString();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                DataTable dt_1 = new DataTable();
                dt_1 = op.getDataTable("select * from drugsProvider where providerId='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'", "users");
                this.txtBoxProviderID.Text = dt_1.Rows[0]["providerId"].ToString();
                this.txtBoxProviderCompany.Text = dt_1.Rows[0]["providerCompany"].ToString();
                this.txtBoxProviderPY.Text = dt_1.Rows[0]["providerPY"].ToString();
                this.txtBoxProviderAdd.Text = dt_1.Rows[0]["providerAddress"].ToString();
                this.tetBoxProviderTel.Text = dt_1.Rows[0]["providerTel"].ToString();
                this.txtBoxProviderName.Text = dt_1.Rows[0]["providerName"].ToString();
                this.txtBoxProviderWeb.Text = dt_1.Rows[0]["providerWebsite"].ToString();
                this.txtBoxProviderMail.Text = dt_1.Rows[0]["providerMailBox"].ToString();
            }
            catch (Exception ex) 
            { 
                MessageBox.Show(ex.Message); 
                return;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.txtBoxProviderID.ReadOnly = true;
            this.txtBoxProviderCompany.ReadOnly = false;
            this.txtBoxProviderAdd.ReadOnly = false;
            this.tetBoxProviderTel.ReadOnly = false;
            this.txtBoxProviderName.ReadOnly = false;
            this.txtBoxProviderWeb.ReadOnly = false;
            this.txtBoxProviderMail.ReadOnly = false;
            this.txtBoxProviderID.Text = "";
            this.txtBoxProviderCompany.Text = "";
            this.txtBoxProviderPY.Text = "";
            this.txtBoxProviderAdd.Text = "";
            this.tetBoxProviderTel.Text = "";
            this.txtBoxProviderName.Text = "";
            this.txtBoxProviderWeb.Text = "";
            this.txtBoxProviderMail.Text = "";
            this.txtBoxProviderID.Focus();
        }

        private void Save()
        {
            try
            {
                string c1 = "";
                string c2 = "";
                string c3 = "";
                string c4 = "";
                string c5 = "";
                string c6 = "";
                string c7 = "";
                string c8 = "";
                string c11 = "";
                DataTable dt1 = new DataTable();
                dt1 = op.getDataTable("select max(providerId) from drugsProvider", "drugsProvider");
                if (dt1 != null && dt1.Rows.Count > 0)
                {
                    c11 = dt1.Rows[0][0].ToString();
                    c11 = Convert.ToString(Convert.ToInt16(c11) + 1);
                }
                c1 = txtBoxProviderID.Text.Trim();
                c2 = txtBoxProviderCompany.Text.Trim();
                c3 = txtBoxProviderPY.Text.Trim();
                c4 = txtBoxProviderAdd.Text.Trim();
                c5 = tetBoxProviderTel.Text.Trim();
                c6 = txtBoxProviderName.Text.Trim();
                c7 = txtBoxProviderWeb.Text.Trim();
                c8 = txtBoxProviderMail.Text.Trim();


                if (txtBoxProviderID.Text.Trim() == "")
                {

                    DataTable dt2 = new DataTable();
                    dt2 = op.getDataTable("select * from drugsProvider where providerId ='" + c1 + "'", "drugsProvider");
                    if (txtBoxProviderCompany.Text == "")
                    {
                        MessageBox.Show("供应商名称不能为空！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    if (txtBoxProviderAdd.Text == "")
                    {
                        MessageBox.Show("供应商地址不能为空！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    if (tetBoxProviderTel.Text == "")
                    {
                        MessageBox.Show("电话不能为空！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    if (txtBoxProviderName.Text == "")
                    {
                        MessageBox.Show("联系人不能为空！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }

                    string sql_a = "insert into drugsProvider(providerCompany,providerPY,providerAddress,providerTel,providerName,providerWebsite,providerMailBox) values ('" + c2 + "','" + c3 + "','" + c4 + "','" + c5 + "','" + c6 + "','" + c7 + "','" + c8 + "')";
                    op.ExecuteSql(sql_a);
                    MessageBox.Show("保存成功！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                    txtBoxProviderID.ReadOnly = true;
                    txtBoxProviderCompany.ReadOnly = true;
                    txtBoxProviderPY.ReadOnly = true;
                    txtBoxProviderAdd.ReadOnly = true;
                    tetBoxProviderTel.ReadOnly = true;
                    txtBoxProviderName.ReadOnly = true;
                    txtBoxProviderWeb.ReadOnly = true;
                    txtBoxProviderMail.ReadOnly = true;

                    txtBoxProviderID.Text = "";
                    txtBoxProviderCompany.Text = "";
                    txtBoxProviderPY.Text = "";
                    txtBoxProviderAdd.Text = "";
                    tetBoxProviderTel.Text = "";
                    txtBoxProviderName.Text = "";
                    txtBoxProviderWeb.Text = "";
                    txtBoxProviderMail.Text = "";

                    DataTable dt = new DataTable();
                    string sql_b = "select providerId 供应商ID,providerCompany 供应商名称,providerPY 拼音码,providerAddress 供应商地址,providerTel 电话,providerName 联系人,providerWebsite 供应商网址,providerMailBox 供应商邮箱 from drugsProvider order by providerId";
                    dt = op.getDataTable(sql_b, "drugsProvider");
                    dataGridView1.DataSource = dt;
                    return;
                }
                string sql_1 = "update drugsProvider set providerCompany='" + c2 + "',providerPY='" + c3 + "',providerAddress='" + c4
                    + "',providerTel='" + c5 + "',providerName='" + c6 + "',providerWebsite='" + c7 + "',providerMailBox='" + c8 + "' where providerId='" + c1 + "'";
                op.ExecuteSql(sql_1);

                MessageBox.Show("修改成功！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                txtBoxProviderID.ReadOnly = true;
                txtBoxProviderCompany.ReadOnly = true;
                txtBoxProviderPY.ReadOnly = true;
                txtBoxProviderAdd.ReadOnly = true;
                tetBoxProviderTel.ReadOnly = true;
                txtBoxProviderName.ReadOnly = true;
                txtBoxProviderWeb.ReadOnly = true;
                txtBoxProviderMail.ReadOnly = true;

                txtBoxProviderID.Text = "";
                txtBoxProviderCompany.Text = "";
                txtBoxProviderPY.Text = "";
                txtBoxProviderAdd.Text = "";
                tetBoxProviderTel.Text = "";
                txtBoxProviderName.Text = "";
                txtBoxProviderWeb.Text = "";
                txtBoxProviderMail.Text = "";

                DataTable dt11 = new DataTable();
                string sql_2 = "select providerId 供应商ID,providerCompany 供应商名称,providerPY 拼音码,providerAddress 供应商地址,providerTel 电话,providerName 联系人,providerWebsite 供应商网址,providerMailBox 供应商邮箱 from drugsProvider";
                dt11 = op.getDataTable(sql_2, "drugsProvider");
                dataGridView1.DataSource = dt11;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
       
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            Save();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.txtBoxProviderID.Text == "")
                {
                    MessageBox.Show("请选择需要删除的内容！","温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                if (MessageBox.Show("确定需要删除供应商[ " + this.txtBoxProviderCompany.Text.Trim() + " ]吗？", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    op.ExecuteSql("delete from  drugsProvider where providerId='" + this.txtBoxProviderID.Text.Trim() + "'");
                    DataTable dt = new DataTable();
                    string sql = "select providerId 供应商ID,providerCompany 供应商名称,providerPY 拼音码,providerAddress 供应商地址,providerTel 电话,providerName 联系人,providerWebsite 供应商网址,providerMailBox 供应商邮箱 from drugsProvider order by providerId";
                    dt = op.getDataTable(sql, "drugsProvider");
                    dataGridView1.DataSource = dt;
                    MessageBox.Show("删除成功！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }


        private void btnChange_Click(object sender, EventArgs e)
        {
            if (this.txtBoxProviderID.Text == "")
            {
                MessageBox.Show("请选择需要修改的内容！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            this.txtBoxProviderCompany.ReadOnly = false;
            this.txtBoxProviderPY.ReadOnly = false;
            this.txtBoxProviderAdd.ReadOnly = false;
            this.tetBoxProviderTel.ReadOnly = false;
            this.txtBoxProviderName.ReadOnly = false;
            this.txtBoxProviderWeb.ReadOnly = false;
            this.txtBoxProviderMail.ReadOnly = false;
            this.txtBoxProviderCompany.Focus();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Dispose();
            this.Close();
        }


        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            this.txtBoxProviderPY.Text = DrugsShopManagement.DBCLASS.Operator.GetSpellCode(this.txtBoxProviderCompany.Text);   
        }

        //private void btnGiveUp_Click(object sender, EventArgs e)
        //{
        //    if (this.txtBoxProviderCompany.ReadOnly == true)
        //    {
        //        return;
        //    }
        //    txtBoxProviderID.ReadOnly = true;
        //    txtBoxProviderCompany.ReadOnly = true;
        //    txtBoxProviderPY.ReadOnly = true;
        //    txtBoxProviderAdd.ReadOnly = true;
        //    tetBoxProviderTel.ReadOnly = true;
        //    txtBoxProviderName.ReadOnly = true;
        //    txtBoxProviderWeb.ReadOnly = true;
        //    txtBoxProviderMail.ReadOnly = true;

        //    txtBoxProviderID.Text = "";
        //    txtBoxProviderCompany.Text = "";
        //    txtBoxProviderPY.Text = "";
        //    txtBoxProviderAdd.Text = "";
        //    tetBoxProviderTel.Text = "";
        //    txtBoxProviderName.Text = "";
        //    txtBoxProviderWeb.Text = "";
        //    txtBoxProviderMail.Text = "";
        //}

      
        private void FrmDurgsProvider_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F2)
            {
                Save();
            }
            
        }

        private void tb_Search_TextChanged(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                string sql = "select providerId 供应商ID,providerCompany 供应商名称,providerPY 拼音码,providerAddress 供应商地址,providerTel 电话,providerName 联系人,providerWebsite 供应商网址,providerMailBox 供应商邮箱 from drugsProvider where providerId like '%" + this.txtBoxProviderSearch.Text.Trim() + "%'or providerCompany like '%" + this.txtBoxProviderSearch.Text.ToUpper() + "%' order by providerId";
                dt = op.getDataTable(sql, "users");
                dataGridView1.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }
       
    }
}
